<?php

namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Session;
use App\Models\Admin;

class AdminMiddleware{

    public function handle($request, Closure $next){
        if (!$request->session()->has('admin.admin_id')) {    //判断是否登录
            //未登录，跳转到登录页面
            return redirect("admin/index/login");
        }
        //已登录，判断权限
        $rule = session("admin.rule");
        $path = $request->path();
        if(in_array($path,$rule)){
            //有权限
            return $next($request);
        }else{
            //无权限
            $pathArr = explode("/",$path);
            $method = $pathArr[2];
            //指定特殊方法（可通过）
            $special = ["center","handle"];
            if(in_array($method,$special) || $request->is("admin/center/index")){ //判断是否是特殊方法
                return $next($request);
            }else{
                if($request->ajax()){
                   return retArray(404,'对不起，您没有权限！');
                }else{
                    abort(404,'对不起，您没有权限！');
                }
            }
        }
    }
}